package com.daiTest.controller;

import com.daiTest.pojo.Headline;
import com.daiTest.service.HeadlineService;
import com.daiTest.untils.Result;
import com.daiTest.untils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("headline")
@CrossOrigin
public class HeadlineController {
    @Autowired
    private HeadlineService headlineService;

    /*发布新闻
     * 登陆以后才可以访问*/
    @PostMapping("publish")
    public Result publish(@RequestBody Headline headline, @RequestHeader String token) {
        Result result = headlineService.publish(headline, token);
        return result;
    }

    /*头条回显*/
    @PostMapping("findHeadlineByHid")
    public Result findHeadlineByHid(Integer hid) {

        Result result = headlineService.findHeadlineByHid(hid);
        return result;
    }

    /*修改新闻*/
    @PostMapping("update")
    public Result update(@RequestBody Headline headline) {
        /*//因为这个接口比较简单 所以直接用service层就可以实现功能
        boolean updateById = headlineService.updateById(headline);
        return Result.ok(null);*/
        Result result = headlineService.Myupdate(headline);
        return result;
    }

    /*删除新闻*/
    @PostMapping("removeByHid")
    public Result removeByHid(Integer hid){
        boolean b = headlineService.removeById(hid);
        if(!b){
            return Result.build(null, ResultCodeEnum.NOTLOGIN);
        }
        return Result.ok(null);
    }
}
